package com.example.db;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import com.example.db.bean.User;
import com.example.db.utils.TimeUtils;
import com.snappydb.DB;
import com.snappydb.DBFactory;
import com.snappydb.SnappydbException;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;

/**
 * Created by kkmike999 on 16/2/12.
 */
public class SnappyTest extends TestCase {

    public static final int    COUNT  = 10000;
    public static final String dbName = "db_" + (int) (Math.random() * 10000);

    private DB     db;
    private User[] users;

    @Before
    public void setUp() throws Exception {

        db = DBFactory.open(DbApp.getContext(), dbName, new Kryo());

        TimeUtils.begin();

        users = UserHelper.createUserList(COUNT);

        System.out.println("生成对象 time = " + TimeUtils.time());
    }

    @After
    public void tearDown() throws SnappydbException {
        db.destroy();
    }

    @Test
    public void openDb() throws SnappydbException {
        db.close();

        for (int i = 0; i < 1000; i++) {
            String dbName = "db_" + (int) (Math.random() * 10000);

//            System.out.println("open " + i);
            try {
                db = DBFactory.open(DbApp.getContext(), dbName, new Kryo());
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void snappyPutInt() throws Exception {

        TimeUtils.begin();

        String[] keys = new String[COUNT];

        for (int i = 0; i < COUNT; i++) {
            keys[i] = "table_int_" + i;
        }

        System.out.println("生成字符串 time = " + TimeUtils.time());

        TimeUtils.begin("put int");

        for (int i = 0; i < COUNT; i++) {
            db.putInt(keys[i], i);
        }

        System.out.println("snappyPutInt time = " + TimeUtils.time("put int"));
    }

    @Test
    public void snappyPutObj() throws SnappydbException {
        TimeUtils.begin();

        for (User user : users) {
            db.put(user.name, user);
        }

        System.out.println("SnappyTest(" + users.length + ") : put Obj time = " + TimeUtils.time());
    }

    @Test
    public void snappyPutBytes() throws SnappydbException {
        TimeUtils.begin();

        Kryo kryo = new Kryo();
        kryo.register(User.class);

        ByteArrayOutputStream bout   = new ByteArrayOutputStream();
        Output                output = new Output(bout);

        for (User user : users) {
            kryo.writeObject(output, user);
            byte[] bytes = bout.toByteArray();

            db.put(user.name, bytes);

            output.clear();
            bout.reset();
        }

        output.close();

        System.out.println("SnappyTest(" + users.length + ") : put bytes time = " + TimeUtils.time());
    }

    @Test
    public void delObj() throws Exception {
        snappyPutObj();// 先插入

        TimeUtils.begin();

        for (User user : users) {
            db.del(user.name);
        }

        System.out.println("SnappyTest(" + users.length + ") : del Obj time = " + TimeUtils.time());
    }

    @Test
    public void getObj() throws SnappydbException {
        snappyPutObj();// 先插入

        TimeUtils.begin();

        for (User user : users) {
            User u = db.getObject(user.name, User.class);
        }

        System.out.println("SnappyTest(" + users.length + ") : get Obj time = " + TimeUtils.time());
    }
}